<template>
	<!-- 20221118 -->
	<view class="mall-search-page">
		<!-- 页面内容 -->
		<view class="page-content">
			<!-- 搜索页 -->
			<ty-layer-search :hot-list="searchHotList" :history-list.sync="searchHistoryList" @search="search"
				@remove="remove">

				<!-- 当页没有标题。APP页面传入导航条以兼容样式 -->
				<!-- #ifdef APP-PLUS -->
				<!-- 占位 view，避免页面内容出现在状态栏 -->
				<view class="height-status-bar"></view>
				<!-- #endif -->

			</ty-layer-search>
		</view>

		<!-- 页面浮层 -->
		<view class="page-layer">

		</view>
	</view>
</template>

<script>
	import mall from "./api/mall.js"
	export default {
		data() {
			return {
				// 热门搜索
				searchHotList: [],
				// 历史搜索
				searchHistoryList: [],
			}
		},
		
		onShow() {
			// 获取历史搜索
			if (this.cn.isLogin()) {
				this.listHistorySearch()
			}
		},
		
		onReady() {
			// 获取热门搜索
			this.listHotSearch()
		},
		
		methods: {
			/**
			 * 获取热门搜索
			 * @date 2022-11-18
			 */
			listHotSearch() {
				mall.listHotSearch().then(res => {
					if (res.code == 0) {
						this.searchHotList = res.data.search_hot_list
					}
				})
			},
			
			/**
			 * 获取历史搜索
			 * @date 2022-11-18
			 */
			listHistorySearch() {
				mall.listHistorySearch().then(res => {
					if (res.code == 0) {
						this.searchHistoryList = res.data.search_history_list
					}
				})
			},
			
			/**
			 * 清空历史搜索
			 * @date 2022-11-18
			 */
			remove() {
				if (!this.searchHistoryList.length) {
					this.cn.toast('暂无历史搜索记录')
					return false
				}
				this.cn.confirm('是否确定清空历史搜索').then(res => {
					if (res.confirm) {
						mall.deleteAllHistorySearch().then(res => {
							if (res.code == 0) {
								this.cn.toast('清空成功')
								// 清空历史搜索
								this.searchHistoryList = []
							}
						})
					}
				})
			},
			
			/**
			 * 搜索
			 * @date 2022-11-18
			 */
			search(e) {
				if (this.cn.isLogin()) {
					mall.insertHistorySearch(e.keyword).then(res => {
						uni.navigateTo({
							url: '/pages/mall/list?keyword=' + e.keyword
						})
					})
				} else {
					uni.navigateTo({
						url: '/pages/mall/list?keyword=' + e.keyword
					})
				}
			}
		}
	}
</script>

<style scoped>

</style>
